package yunquan.wang.modules.base.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.base.entity.DynamicColumn;
import yunquan.wang.modules.base.entity.DynamicReport;
import yunquan.wang.modules.base.service.DynamicReportService;

@Controller
@RequestMapping("/core/dynamicreport")
public class DynamicReportController {
	@Resource
	private DynamicReportService service;

	@RequestMapping(value = "/list.do")
	public String list(HttpServletRequest request, Model model) {
		Pageable page = WebHelper.getPage(request);
		List<PropertyFilter> filters = WebHelper.filterRequest(request);
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,
				service.findPage(page, filters));
		return "core/dynamicreportList";
	}

	@RequestMapping(value = "/save.do")
	public @ResponseBody
	AjaxDone save(@Valid DynamicReport entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.save(entity);
			} catch (Exception e) {
				return WebHelper.saveFail(e);
			}
		}
		AjaxDone done = WebHelper.saveSuccess();

		done.setCallbackType(AjaxDone.CB_FORWARD);
		done.setForwardUrl("core/dynamicreport/column.do?reportId="
				+ entity.getId());
		return done;
	}

	@RequestMapping(value = "/column.do")
	public String buildColumnParam(String reportId, Model model) {
		List<DynamicColumn> columns = service.getQueryColumn(reportId);
		model.addAttribute("reportId", reportId);
		model.addAttribute("colomns", columns);
		return "core/dynamicreportColumn";
	}

	@RequestMapping(value = "/input.do")
	public String input(String id, Model model, String parentId) {
		DynamicReport entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new DynamicReport();
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "core/dynamicreportInput";
	}
	@RequestMapping(value = "/generate.do")
	public String generateReport(HttpServletRequest request, String reportId) {
		String names[] = request.getParameterValues("name");
		DynamicReport entity = service.get(reportId);
		String groupNames = "";
		String subTotals = "";
		List<DynamicColumn> columnList=new ArrayList<DynamicColumn>();
		for (String name : names) {
			String group = request.getParameter(name + "_group");
			groupNames += group + ",";
			String total = request.getParameter(name + "_total");
			subTotals += total + ",";
			String title = request.getParameter(name + "_title");
			String type = request.getParameter(name + "_type");
			String pattern = request.getParameter(name + "_pattern");
			DynamicColumn column = new DynamicColumn();
			column.setName(name);
			column.setPattern(pattern);
			column.setTitle(title);
			column.setType(type);
			columnList.add(column);
		}
		if (groupNames.length() > 1) {
			groupNames = groupNames.substring(0, groupNames.length() - 1);
		}
		if (subTotals.length() > 1) {
			subTotals = subTotals.substring(0, subTotals.length() - 1);
		}
		if (StringUtils.isNotBlank(subTotals)) {
			entity.setSubtotals(subTotals);
		}

		if(StringUtils.isNotBlank(groupNames))
		{
			entity.setGroups(groupNames);
		}
		  try {
			entity	=service.generateColumn(entity,columnList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@RequestMapping(value = "/delete.do")
	public @ResponseBody
	AjaxDone delete(String[] id) {
		for (String ids : id) {
			service.delete(ids);
		}
		return WebHelper.deleteSuccess();
	}

}
